package system.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.TrayIcon.MessageType;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.Border;

import net.ericaro.surfaceplotter.JSurfacePanel;

import org.math.plot.PlotPanel;

import system.activity.MainListener;
import user.activity.MainWindow;
import user.view.Controls;
import user.view.PicturePanelImpl;
import user.view.VisualizerImpl;

public class MainWindowImpl extends JFrame implements MainWindow
{

	JLabel status = new JLabel();
	MainMenuBar menu;
	Controls leftPanel;
	JPanel eastPanel;
	private MainListener listener;
	private VisualizerImpl picturePanel;
	private Timer timer;
	private ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();

	public MainWindowImpl()
	{
		this.setTitle("Quick - DOE version 2.0");
		this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
		this.setLayout(new java.awt.BorderLayout());
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		init();
	}

	public void init()
	{
		Image icon = null;
        try {
            icon = ImageIO.read(new File(
            "src/system/resource/icon.gif"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        setIconImage(icon);
        
		Border border = BorderFactory.createEtchedBorder(Color.white, new Color(178, 178, 178));
		status.setBorder(border);
		status.setOpaque(true);// фон строки
		this.add(status, BorderLayout.SOUTH);
		menu = new MainMenuBar(status);
		this.setJMenuBar(menu);
		this.repaint();
		this.show();
		
		java.awt.event.ActionListener actionListener = new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent actionEvent)
			{
				status.setText(status.getText()+".");
			}
		};
		timer = new Timer(25, actionListener);
		timer.setRepeats(true);
	}


	public void setListener(MainListener listener)
	{
		System.out.println("setListener impl");
		this.listener = listener;
		menu.setListener(listener);
	}

	public void addPanel(JPanel panel)
	{
		this.add(panel, BorderLayout.CENTER);
		repaint();
		show();
	}

	@Override
	public void updateImagePanel(BufferedImage image, String name)
	{
		PicturePanelImpl imagePanel = new PicturePanelImpl(listener);
		if (picturePanel == null) {

			images.add(image);
			picturePanel = new VisualizerImpl();
			picturePanel.setListener(listener);
			this.add(picturePanel, BorderLayout.CENTER);
			imagePanel.setImage(image);
			picturePanel.addPicture(imagePanel, name);

		} else {
			images.add(image);
			imagePanel.setImage(image);
			picturePanel.addPicture(imagePanel, name);
		}
		repaint();
		show();
	}

	@Override
	public void updateImagePanel(File imageFile) throws IOException
	{
		updateImagePanel(ImageIO.read(imageFile), imageFile.getName());
	}

	public void addRightPanel(JPanel panel)
	{
		if (eastPanel != null) {
			this.remove(eastPanel);
		}
		eastPanel = panel;
		this.add(eastPanel, BorderLayout.EAST);
		repaint();
		show();
	}

	public void clearRightPanel()
	{
		eastPanel.removeAll();
	}

	public void addLeftPanel(Controls panel)
	{
		if (leftPanel != null) {
			this.remove(leftPanel);
		}
		leftPanel = (Controls) panel;
		this.add(leftPanel, BorderLayout.WEST);
		repaint();
		show();
	}

	@Override
	public void clearCenterPanel()
	{
		images.clear();
		if (picturePanel != null) {
			remove(picturePanel);
			picturePanel = null;
		}
		repaint();
		show();
	}

	@Override
	public void clearLeftPanel()
	{
		leftPanel.removeAll();
	}

	public Collection<String> getControlDescription()
	{
		return leftPanel.getControlsValues();
	}

	public Collection<BufferedImage> getImages()
	{
		return images;
	}
	public void setStatusText(String text)
	{
		status.setText(text);
	}
	public String getStatusText()
	{
		 return status.getText();
	}
	public Timer getTimer()
	{
		return timer;
	}

	public String getControlHistoryDescription()
	{
		System.out.println("getControlHistoryDescription" + leftPanel.getDescriptionForHistory());
		return leftPanel.getDescriptionForHistory();
	}

	public void showErrorMessage(String message) {
		JOptionPane.showMessageDialog(this, message, "Ошибка", 0);
	}

	@Override
	public void updateImagePanel(JSurfacePanel panel, String name) {
		System.out.println("!");
		if (picturePanel == null) {

			picturePanel = new VisualizerImpl();
			picturePanel.setListener(listener);
			this.add(picturePanel, BorderLayout.CENTER);
			picturePanel.addPanel(panel, name);

		} else {
			picturePanel.addPanel(panel, name);
		}
		repaint();
		show();
	}
	
	@Override
	public void updateImagePanel(PlotPanel panel, String name) {
		if (picturePanel == null) {

			picturePanel = new VisualizerImpl();
			picturePanel.setListener(listener);
			this.add(picturePanel, BorderLayout.CENTER);
			picturePanel.addPanel(panel, name);

		} else {
			picturePanel.addPanel(panel, name);
		}
		repaint();
		show();
	}

}
